// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const db = cloud.database()
  const collection = db.collection('student')

  try {
  
    const records = await collection.get()
    const updatedRecords = []

    
    for (let record of records.data) {
      const fieldName1 = 'account'
      const fieldName2 = 'password'
      const fieldName3 = 'idNumber'
      if (record[fieldName1] && record[fieldName1].length > 0) {
        record[fieldName1] = record[fieldName1].slice(1),
        record[fieldName2] = record[fieldName2].slice(1),
        record[fieldName3] = record[fieldName3].slice(1)
        updatedRecords.push(record)
      }
    }
    for (let updatedRecord of updatedRecords) {
      await collection.doc(updatedRecord._id).update({
        data: {
          [fieldName1]: updatedRecord[fieldName1],
          [fieldName2]: updatedRecord[fieldName2],
          [fieldName3]: updatedRecord[fieldName3]
        }
      })
    }
    return {
      success: true,
      message: '成功更新记录'
    }
  } catch (error) {
    console.error(error)
    return {
      success: false,
      message: error,
      error: error
    }
  }
}